﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Locallab.Ack999Helper.BusinessContextReader
{
    [Serializable]
    internal class FileHeaderDataReader
    {        
        FileHeaderData delimiter;
        string strHeaderData;                
               
        public FileHeaderData Delimiter { get { return delimiter; } }

        internal FileHeaderDataReader(string strHeaderData)
        {            
            delimiter = new FileHeaderData();
            this.strHeaderData = strHeaderData;            
        }

        internal bool ValidateHeader()
        {
            var bResult = false;                       

            try
            {
                var chArrayTemp = strHeaderData.ToCharArray();
                this.delimiter.ElementDelimiter = Convert.ToString(chArrayTemp[3]);
                this.delimiter.ComponentDelimiter = Convert.ToString(chArrayTemp[104]);

                if (chArrayTemp[106] == '\r' && chArrayTemp[107] == '\n')
                    this.delimiter.SegmentDelimiter = new string(chArrayTemp, 105, 3);
                else if (chArrayTemp[105] == '\r' && chArrayTemp[106] == '\n')
                    this.delimiter.SegmentDelimiter = new string(chArrayTemp, 105, 2);
                else
                    this.delimiter.SegmentDelimiter = new string(chArrayTemp, 105, 1);

                var strISA = strHeaderData.Substring(0, strHeaderData.IndexOf(this.delimiter.SegmentDelimiter));

                strHeaderData = strHeaderData.Substring(strHeaderData.IndexOf(this.delimiter.SegmentDelimiter) + 1);

                var strGS = strHeaderData.Substring(0, strHeaderData.IndexOf(this.delimiter.SegmentDelimiter));
                
                bResult = ValidateISA(strISA);

                if (bResult)
                    bResult = ValidateGS(strGS);                

            }
            catch (Exception exValidateHeader)
            {   
                System.Diagnostics.Trace.WriteLine("Error - FileHeaderDataReader:ValidateHeader " + exValidateHeader.ToString());

                Console.WriteLine("Error - FileHeaderDataReader:ValidateHeader " + exValidateHeader.ToString());
                bResult = false;
            }            

            return bResult;
        }

        private bool ValidateISA(string strEdiSegment)
        {
            var bResult = false;            

            try
            {
                var strArrayElements = Utility.SplitSegment(strEdiSegment, this.delimiter.ElementDelimiter);

                if(strArrayElements.Length != 17 || strArrayElements.Length < 1)
                    return false;

                if(strArrayElements[0] != "ISA")
                    return false;                

                bResult = true;
            }
            catch (Exception exValidateISA)
            {
                System.Diagnostics.Trace.WriteLine("Error - FileHeaderDataReader:ValidateISA " + exValidateISA.ToString());
                Console.WriteLine("Error - FileHeaderDataReader:ValidateISA " + exValidateISA.ToString());
                bResult = false;
            }            

            return bResult;
        }

        private bool ValidateST(string strEdiSegment)
        {
            var bResult = false;            

            try
            {
                var strArrayElements = Utility.SplitSegment(strEdiSegment, this.delimiter.ElementDelimiter);

                if (strArrayElements[0] != "ST")
                    return false;
            }
            catch (Exception exValidateISA)
            {
                System.Diagnostics.Trace.WriteLine("Error - FileHeaderDataReader:ValidateST " + exValidateISA.ToString());
                Console.WriteLine("Error - FileHeaderDataReader:ValidateST " + exValidateISA.ToString());
                bResult = false;
            }

            return bResult;
        }

        private bool ValidateGS(string strEdiSegment)
        {
            var bResult = false;

            strEdiSegment = strEdiSegment.Trim();
            
            try
            {
                var strArrayElements = Utility.SplitSegment(strEdiSegment, this.delimiter.ElementDelimiter);

                if(strArrayElements.Length != 9 || strArrayElements.Length < 1)
                    return false;

                if(strArrayElements[0] != "GS")
                    return false;

                this.delimiter.Version = !strArrayElements[8].Equals(null) ? strArrayElements[8].Trim() : string.Empty;

                bResult = true;
            }
            catch (Exception exValidateGS)
            {
                System.Diagnostics.Trace.WriteLine("Error - FileHeaderDataReader:ValidateGS " + exValidateGS.ToString());
                Console.WriteLine("Error - FileHeaderDataReader:ValidateGS " + exValidateGS.ToString());
                bResult = false;
            }            

            return bResult;
        }

        public void SaveFileHeaderInformation()
        {
            EdiFileInformation ediFile = new EdiFileInformation(Guid.NewGuid().ToString());
            ediFile.BeginEdiFileInformationActivity();

            ediFile.FileID = delimiter.FileID;
            ediFile.FileName = delimiter.FileName;
            ediFile.Sender = delimiter.Sender;
            ediFile.ReceiveDate = delimiter.ReceivedDate;
            ediFile.Receiver = delimiter.Receiver;
            ediFile.SegmentDelimiter = delimiter.SegmentDelimiter;
            ediFile.ElementDelimiter = delimiter.ElementDelimiter;
            ediFile.ComponentDelimiter = delimiter.ComponentDelimiter;

            ediFile.CommitEdiFileInformationActivity();
            ediFile.EndEdiFileInformationActivity();            
        }
    } 
}
